package ace.module.account.core.impl.service.member.impl;

import ace.cmp.core.model.PageDto;
import ace.cmp.core.model.PageResultDto;
import ace.cmp.spring.data.jdbc.querydsl.core.converter.PageConverter;
import ace.module.account.api.model.dto.IamAccountDetailDto;
import ace.module.account.api.model.input.AppOutInfoInput;
import ace.module.account.api.model.input.IamAccountCommonInfoOutInput;
import ace.module.account.api.model.input.IamAccountFilterInput;
import ace.module.account.api.model.input.IamAccountModifyStatusInput;
import ace.module.account.api.model.input.IamAccountSearchOutInput;
import ace.module.account.api.model.input.register.RegisterQuickByPhoneNoOutInput;
import ace.module.account.api.model.input.register.RegisterQuickByPhoneOutInput;
import ace.module.account.core.impl.manager.IamAccountManager;
import ace.module.account.core.impl.service.IamAccountService;
import ace.module.account.core.impl.service.member.MemberService;
import ace.module.common.api.constant.SystemRoleConstants;
import ace.module.common.api.model.input.PageInput;
import ace.module.common.api.model.input.TenantInput;
import ace.module.common.api.model.input.TenantPageInput;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

/**
 * 会员服务.
 *
 * @author Caspar
 */
@AllArgsConstructor
@Component
public class MemberServiceImpl implements MemberService {

  private final IamAccountService iamAccountService;
  private final IamAccountManager iamAccountManager;
  private final PageConverter pageConverter;

  @Override
  public Page<IamAccountDetailDto> adminSearch(
      TenantPageInput<IamAccountFilterInput> tenantPageInput) {

    PageDto pageDto = tenantPageInput.getPage();

    Pageable pageable = pageConverter.toPageRequest(pageDto);

    IamAccountSearchOutInput searchInput = IamAccountSearchOutInput.builder()
        .filter(tenantPageInput.getData())
        .appOutInfo(
            AppOutInfoInput.builder()
                .outId(tenantPageInput.getTenantId().toString())
                .outType(SystemRoleConstants.MEMBER)
                .build()
        )
        .build();

    PageInput<IamAccountSearchOutInput> input = PageInput.<IamAccountSearchOutInput>builder()
        .data(searchInput)
        .page(pageDto)
        .build();

    PageResultDto<IamAccountDetailDto> pageResultDto = iamAccountService.searchWithProfileByOutInfo(input);

    return pageConverter.toPage(pageResultDto, pageable);
  }

  @Override
  public Long adminAddByPhone(TenantInput<RegisterQuickByPhoneNoOutInput> input) {
    RegisterQuickByPhoneOutInput outInput = RegisterQuickByPhoneOutInput.builder()
        .base(input.getData())
        .outInfo(AppOutInfoInput.of(SystemRoleConstants.MEMBER,
            input.getTenantId().toString()))
        .build();
    return iamAccountService.registerQuickByPhoneOutInput(outInput);
  }

  @Override
  public void adminModifyCommonInfo(IamAccountCommonInfoOutInput input) {
    iamAccountService.modifyCommonInfo(input);
  }

  @Override
  public void adminModifyStatus(IamAccountModifyStatusInput input) {
    iamAccountService.modifyStatus(input);
  }

  @Override
  public void adminRemove(List<Long> accountIds) {
    iamAccountManager.removeLogic(accountIds);
  }
}
